#!/usr/bin/make -f
# debian/rules for MSEGUI-MSEIDE

DEB_PACKAGE_NAME = mseide-msegui
DEB_DH_BUILDDEB_ARGS := -- -Z bzip2
export DH_ALWAYS_EXCLUDE := COPYING:LICENSE
export LANG:=C

# Define FPC
ifndef FPC
FPC=/usr/bin/fpc
endif
# Set FPCVER
FPCVER=$(shell ${FPC} -iV)

FPCTARGET=$(CPU_TARGET)-linux
# Get version information from changelog file
DEB_VERSION:=$(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_UPSTREAM_VERSION:=$(shell echo $(DEB_VERSION) | cut -f 1 -d -)
#DEB_UPSTREAM_MAIN_VERSION:=$(shell echo ${DEB_UPSTREAM_VERSION} | sed -e 's/^\([0-9\.]*\).*/\1/')
DEB_BUILD=$(lastword $(subst -, ,${DEB_VERSION}))
ifndef PACKAGESUFFIX
#export PACKAGESUFFIX=-${DEB_UPSTREAM_MAIN_VERSION}
endif
# Get directories
CURDIR:=$(shell pwd)
BUILD_DIR=${CURDIR}/debian/build
INSTALL_DIR=${CURDIR}/debian/tmp
BIN_DIR=${INSTALL_DIR}/usr/bin
LIB_DIR=${INSTALL_DIR}/usr/lib/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
DOC_DIR=${INSTALL_DIR}/usr/share/doc/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
SRC_DIR=${INSTALL_DIR}/usr/src/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
# Get utils
ifndef MKDIR
MKDIR=mkdir -p
endif
ifndef CP
CP=cp -Rfpl
endif
ifndef MV
MV=mv
endif
RM:=rm -rf
# Get fpcmake from path if none is specified.
ifndef FPCMAKE
FPCMAKE=fpcmake
endif
# Define FPCDIR if it was not set
ifndef FPCDIR
export FPCDIR=/usr/share/fpcsrc/${FPCVER}
endif
ifndef LAZBUILD
LAZBUILD=${CURDIR}/lazbuild
endif
# Set default compilation options
BUILDOPTS=USESVN2REVISIONINC=0
INSTALLOPTS=INSTALL_PREFIX=$(INSTALL_DIR)/usr
# Configure patch system
include /usr/share/quilt/quilt.make

#export DH_VERBOSE=1

###################
# Clean
#

clean: patch configure clean-patched unpatch
	${RM} build-arch-stamp install-arch-stamp arrange-arch-stamp configure-stamp
	${RM} build-doc-stamp install-doc-stamp
	${RM} build-indep-stamp install-indep-stamp
	${RM} debian-files-stamp
	${RM} debian/files debian/docs debian/dirs debian/*.files debian/*.docs debian/*.examples debian/*.postinst debian/*.postrm
	${RM} .pc
	# Remove auto-generated version file.
	${RM} ide/version.inc
	# Remove auto-generated make files.
	find -name Makefile.fpc -execdir 'sh' '-c' '${RM} $$(basename {} .fpc)' ';'
	dh_clean
clean-patched:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	${RM} apps/ide/mseide tools/bmp2pas tools/form2pas tools/i18n/msei18n
	find -regex '.*\.\(a\|or?\|so\.*\|ppu\)' -delete
	dh_prep

###################
# Debian files
#

debian-files: patch debian-files-stamp
debian-files-stamp:
	@echo "--- Creating/fixing *.install files"
	#/bin/bash debian/fixdeb debian $(FPCTARGET) $(PPNEW)
	touch debian-files-stamp

###################
# Arch packages
#

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Remove auto-generated make files.
	find -name Makefile.fpc -execdir 'sh' '-c' '${RM} $$(basename {} .fpc)' ';'
	# Regenrate make files using fpcmake.
	#${FPCMAKE} -r -Tall
	find -regex '.*\.\(pas\|inc\|mfm\|jpg\|png\|xpm\|sdef\|prj\)' -exec chmod -x '{}' ';'
	touch configure-stamp

build-arch: debian-files configure build-arch-stamp
build-arch-stamp:
	@echo "--- Building"
	dh_testdir
	sh ${CURDIR}/debian/build_all.sh
	touch build-arch-stamp

install-arch: build-arch install-arch-stamp
install-arch-stamp:
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_installdirs
	# create all necessary directories
	${MKDIR} ${BIN_DIR}
	${MKDIR} ${LIB_DIR}/units/common
	${MKDIR} ${LIB_DIR}/apps/ide
	${MKDIR} ${LIB_DIR}/tools/i18n
	# Install the IDE & its accessories
	install -m 755 ${CURDIR}/apps/ide/mseide ${LIB_DIR}/apps/ide
	cp -Rfpl -t ${LIB_DIR}/apps/ide apps/ide/syntaxdefs apps/ide/templates
	# Install the tools
	install -m 755 ${CURDIR}/tools/bmp2pas ${LIB_DIR}/tools/bmp2pas
	install -m 755 ${CURDIR}/tools/form2pas ${LIB_DIR}/tools/form2pas
	install -m 755 ${CURDIR}/tools/i18n/msei18n ${LIB_DIR}/tools/i18n/msei18n
	# Install the library files
	find -name '*.ppu' -or -name '*.[oa]' -or -name '*.so*' | egrep -ve "\/(designutils|apps|tools)\/" | xargs cp -Rfpl -t ${LIB_DIR}/units/common
	# Install startup scripts & links
	install -m 755 ${CURDIR}/install/mseide ${BIN_DIR}/mseide
	install -m 755 ${CURDIR}/install/mseide-icewm ${BIN_DIR}/mseide-icewm
	# copy icons and menu entries for the GNOME menu
	${MKDIR} $(INSTALL_DIR)/usr/share/pixmaps
	${MKDIR} $(INSTALL_DIR)/usr/share/applications
	#convert -geometry 32x32 ${CURDIR}/images/mseide_32x32.png $(INSTALL_DIR)/usr/share/pixmaps/mseide.xpm
	cp ${CURDIR}/images/mseide_32x32.xpm $(INSTALL_DIR)/usr/share/pixmaps/mseide.xpm
	chmod 644 $(INSTALL_DIR)/usr/share/pixmaps/*.xpm
	install -m 644 ${CURDIR}/install/mseide.desktop $(INSTALL_DIR)/usr/share/applications/mseide.desktop
	# copy the contrib area
	${MKDIR} ${LIB_DIR}/contrib
	cp -Rfpl -t ${LIB_DIR}/contrib ${CURDIR}/contrib/*
	# copy the misc area
	${MKDIR} ${LIB_DIR}/misc
	cp -Rfpl -t ${LIB_DIR}/misc ${CURDIR}/misc/*
	chmod 755 ${LIB_DIR}/misc/xft2gs/do_xft.sh
	chmod 755 ${LIB_DIR}/misc/svn_upd_script/updatefromsvn
	chmod 755 ${LIB_DIR}/misc/doc_gen/fpc/*.sh
	chmod 755 ${LIB_DIR}/misc/doc_gen/msegui/*.sh
	# Install lintian override files
	set -x -e ; cd debian/overrides ; for PACKAGE in * ; do \
	  if [ -d ../$${PACKAGE}${PACKAGESUFFIX} ] ; \
	  then \
	  	${MKDIR} ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides ; \
	    sed -e "s/$${PACKAGE}/&${PACKAGESUFFIX}/g" $${PACKAGE} > ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides/$${PACKAGE}${PACKAGESUFFIX} ; \
	  fi ; \
	done
ifeq (${DEB_BUILD},0)
	# We are building upstream debs, install override files for build = 0
	set -x -e ; cd debian ; for PACKAGE in `sed -n -e 's/Package: \(.*\)/\1/p' control` ; do \
	  if [ -d $${PACKAGE} ] ; \
	  then \
	    ${MKDIR} $${PACKAGE}/usr/share/lintian/overrides ; \
	    echo "$${PACKAGE}: debian-revision-should-not-be-zero" >> $${PACKAGE}/usr/share/lintian/overrides/$${PACKAGE} ; \
	  fi ; \
	done
endif
	find ${INSTALL_DIR} -empty -delete
	touch install-arch-stamp

arrange-arch: install-arch arrange-arch-stamp install-man
arrange-arch-stamp:
	dh_testdir
	dh_testroot
	dh_install -s --sourcedir=debian/tmp --list-missing
	touch arrange-arch-stamp

build-indep: patch
install-indep:
	touch install-indep-stamp

install-man: install-man-stamp
install-man-stamp:
	# Install man pages
	#$(MAKE) -C install/man installman $(INSTALLOPTS)
	#for s in ${INSTALL_DIR}/usr/man/man[1-9] ; do cd $${s} && for f in *.[1-9] ; do ${MV} $${f} $${f%%.*}-${DEB_UPSTREAM_MAIN_VERSION}."$${f#*.}" ; done ; done || true
	dh_installman -s
	${RM} ${INSTALL_DIR}/usr/man
	touch install-man-stamp

###################
# Documentation
#

build-doc: patch debian-files build-doc-stamp
build-doc-stamp:
	@echo "--- Building Documentation"
	dh_testdir
	#$(MAKE) -C docs $(CONVERTER) $(BUILDOPTS) FPDOC=$(NEWFPDOC)
	touch build-doc-stamp

install-doc: build-doc install-doc-stamp
install-doc-stamp:
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	${MKDIR} ${DOC_DIR}
	install -m 644 ${CURDIR}/README.TXT ${DOC_DIR}
	install -m 644 ${CURDIR}/VERSION.TXT ${DOC_DIR}
	install -m 644 ${CURDIR}/ZEOS.TXT ${DOC_DIR}
	cp -Rfpl -t ${DOC_DIR} ${CURDIR}/docs/*
	touch install-doc-stamp

###################
# Source
#

install-source: install-source-stamp
install-source-stamp:
	@echo "--- Cleaning the tree and copying the source code"
	dh_testdir
	dh_testroot
	${MKDIR} ${SRC_DIR}
	cp -Rfpl -t ${SRC_DIR} \
	${CURDIR}/apps \
	${CURDIR}/lib \
	${CURDIR}/patch_* \
	${CURDIR}/tools \
	${CURDIR}/debian/build_all.sh \
	${CURDIR}/debian/clean_all.sh \
	${CURDIR}/README.TXT \
	${CURDIR}/VERSION.TXT \
	${CURDIR}/ZEOS.TXT
	find ${SRC_DIR} -regex '.*\.\(bmp\|jpg\|png\|xpm\)' -delete
	$(RM) ${SRC_DIR}/components/lazreport/license*.txt
	find ${SRC_DIR} -empty -delete
	touch install-source-stamp

###################
# Generic
#

build: build-arch build-indep
install: install-arch install-indep
binary: binary-arch binary-indep


###################
# Deb building
#

binary-indep: clean-patched build-doc install-doc install-source debian-files
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i -X.in
	dh_installchangelogs -i
	dh_install -i --sourcedir=debian/tmp --list-missing
	# Install lintian override files
	set -x -e ; cd debian/overrides ; for PACKAGE in * ; do \
	  if [ -d ../$${PACKAGE}${PACKAGESUFFIX} ] ; \
	  then \
	  	${MKDIR} ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides ; \
	    sed -e "s/$${PACKAGE}/&${PACKAGESUFFIX}/g" $${PACKAGE} > ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides/$${PACKAGE}${PACKAGESUFFIX} ; \
	  fi ; \
	done
ifeq (${DEB_BUILD},0)
	# We are building upstream debs, install override files for build = 0
	set -x -e ; cd debian ; for PACKAGE in `sed -n -e 's/Package: \(.*\)/\1/p' control` ; do \
	  if [ -d $${PACKAGE} ] ; \
	  then \
	    ${MKDIR} $${PACKAGE}/usr/share/lintian/overrides ; \
	    echo "$${PACKAGE}: debian-revision-should-not-be-zero" >> $${PACKAGE}/usr/share/lintian/overrides/$${PACKAGE} ; \
	  fi ; \
	done
endif
	dh_compress -i -X.pdf
	dh_installdebconf -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_fixperms -i
	dh_md5sums -i
	dh_builddeb -i $(DEB_DH_BUILDDEB_ARGS)

binary-arch: arrange-arch
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs -a -X.in
	dh_installchangelogs -a
	dh_installexamples -a
	dh_installmenu
	fpc-depends
	dh_strip -s
	dh_compress -a
	dh_installdebconf -a
	dh_installdeb -a
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_fixperms -a
	dh_md5sums -s
	dh_builddeb -s $(DEB_DH_BUILDDEB_ARGS)

.PHONY: build clean binary binary-arch \
	binary-indep debian-files build-arch \
	install install-indep install-arch \
	configure

get-orig-source:
	-uscan --upstream-version=0 --rename
